package iot.ht.controller.htrole.service;

import iot.core.data.*;
import iot.core.data.entity.*;
import iot.core.data.mapper.*;
import iot.core.util.StreamUtil;
import iot.ht.controller.htrole.data.HTRoleDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class HTRoleService extends ServiceImpl<HTRoleMapper, HTRole> implements IHTRoleService {
    @Autowired
    HTDBMapper htdbMapper;

    @Autowired
    HTRoleMenuMapper htRoleMenuMapper;
    @Autowired
    HTPlatMapper HTPlatMapper;

    @Autowired
    HTDeptMapper htDeptMapper;

    @Override
    public HTRoleDto InsertInit(){
        var mm=new HTRoleDto();
        var qw=HTPlatMapper.getQueryWrapper();
        qw.eq("Id",1000);
        var mplat= HTPlatMapper.selectOne( qw );
        mm.setHTPlat_Id(mplat.getId());
        mm.setHTPlat_Name(mplat.getName() );
        return mm;
    }

    @Override
    public HTRoleDto Detail(BaseQto qm){
        var m=getBaseMapper().selectById(qm.getId());
        var mm=new HTRoleDto();
        getBaseMapper().entityToDto(m,mm);
        mm.setHTPlat_Name( htdbMapper.getName("htplat",m.getHTPlat_Id() ) );
        mm.setCrt_UName( htdbMapper.getName("htuser",m.getCrt_UId() ) );
        mm.setCrt_DName( htdbMapper.getName("htdept",m.getCrt_DId() ) );

        var qwrolemenu=htRoleMenuMapper.getQueryWrapper();
        qwrolemenu.eq("HTRole_Id",mm.getId() );
        qwrolemenu.select("HTMenu_Id");
        var lmenuid=htRoleMenuMapper.selectMaps(qwrolemenu);
        for(var mmap:lmenuid){
            mm.getHTMenu_Ids().add(  (Long)(mmap.get("HTMenu_Id"))  );
        }

        return mm;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int Insert(HTRoleDto mm){

        var m = new HTRole();
        getBaseMapper().dtoToEntity(mm,m);

        for(var mmid: mm.getHTMenu_Ids())
        {
            var madst=new HTRoleMenu();
            madst.setId(htdbMapper.nextLong() );
            madst.setHTRole_Id(mm.getId() );
            madst.setHTMenu_Id(mmid );
            htRoleMenuMapper.insert(madst,true);
        }
        return getBaseMapper().insert(m,true);

    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public   int Update(HTRoleDto mm){
        var m=getBaseMapper().selectById(mm.getId());

        getBaseMapper().dtoToEntity(mm,m);

        var qwrolemenu=htRoleMenuMapper.getQueryWrapper();
        qwrolemenu.eq("HTRole_Id",mm.getId() );
        var lold=htRoleMenuMapper.selectList(qwrolemenu);
        for(var mmid: mm.getHTMenu_Ids())
        {
            var madst = StreamUtil.get(lold,a->a.getHTMenu_Id().equals(mmid));
            if(ObjectUtils.isEmpty(madst)){
                madst=new HTRoleMenu();
                madst.setId(htdbMapper.nextLong() );
                madst.setHTRole_Id(mm.getId() );
                madst.setHTMenu_Id(mmid );
                htRoleMenuMapper.insert(madst,true);
            }
            else{

            }
        }



        for(var madb : lold)
        {
            var masrc= StreamUtil.get(  mm.getHTMenu_Ids(), a->a.equals( madb.getHTMenu_Id()) );
            if(ObjectUtils.isEmpty(masrc)){
                htRoleMenuMapper.deleteById(madb,true);
            }
        }

        return getBaseMapper().updateById(m);
    }


    @Override

    public PageData<HTRoleDto> Page(BasePageDTQto qm){
       var qw=getBaseMapper().getQueryWrapper();
        if(StringUtils.hasText(qm.getDTB() )){
            qw.ge("Edt_DT",qm.getDB() );
        }

        if(StringUtils.hasText(qm.getDTE() )){
            qw.lt("Edt_DT",qm.getDE() );
        }

        if(StringUtils.hasText(qm.getKey() )){
            qw.like("Name",qm.getKey() );
        }
        var sort=qm.getSort();
        qw.orderBy(true,sort.isIsAsc(),sort.getFiled() );


        var r=new PageData<HTRoleDto>();
        var r1=getBaseMapper().page( qw,qm );
        r.setTotal(r1.getTotal());
        var l1=r1.getData().stream().map(a->a.getCrt_UId() ).distinct().collect(Collectors.toList());
        var l2=r1.getData().stream().map(a->a.getHTPlat_Id() ).distinct().collect(Collectors.toList());
        var l3=r1.getData().stream().map(a->a.getCrt_DId() ).distinct().collect(Collectors.toList());

        var luname=htdbMapper.getNames("htuser",l1);
        var ldname=htdbMapper.getNames("htdept",l2);
        var lpname=htdbMapper.getNames("htplat",l2);

        var data=new ArrayList<HTRoleDto>();
        for(var m:r1.getData()){
            var mm=new HTRoleDto();
            getBaseMapper().entityToDto(m,mm);
            mm.setCrt_UName(
                    StreamUtil.getOrDefault(luname,a->a.getId().equals(m.getCrt_UId()), IdNameDto.class).getName()
            );

            mm.setHTPlat_Name(
                    StreamUtil.getOrDefault(lpname,a->a.getId().equals(m.getHTPlat_Id()), IdNameDto.class).getName()
            );

            mm.setCrt_DName(
                    StreamUtil.getOrDefault(ldname,a->a.getId().equals(m.getCrt_DId()), IdNameDto.class).getName()
            );
            data.add(mm);
        }
        r.setData(data);
        return r;
    }

    public List<IdNameDto> ListByDept_Id(BaseQto mm){
        var mdept=getBaseMapper().getById(mm.getId() );
        var qw=getBaseMapper().getQueryWrapper();
        qw.eq("HTPlat_Id",mdept.getHTPlat_Id() );
        qw.select("Id","Name");
        var r=new ArrayList<IdNameDto>();
        var l=getBaseMapper().selectMaps(qw);
        for (var mmap:l ) {
            var m=new IdNameDto();
            m.setId(  (Long)(mmap.get("Id")) );
            m.setName(  (String) (mmap.get("Name")) );
            r.add(m);
        }
        return r;
    }

}
